package com.lanxin.hr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanxin.hr.entity.JcBasicdatavalue;
import com.lanxin.hr.mapper.JcBasicdatavalueMapper;
import com.lanxin.hr.service.IJcBasicdatavalueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.QueryEval;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-18
 */
@Service
public class JcBasicdatavalueServiceImpl extends ServiceImpl<JcBasicdatavalueMapper, JcBasicdatavalue> implements IJcBasicdatavalueService {

    @Autowired
    private JcBasicdatavalueMapper jcBasicdatavalueMapper;

    @Override
    //列表所有基础数据,使用mybatis-plus的分页查询
    public IPage<JcBasicdatavalue> listBasicdatavalue(Integer page,Integer pageSize,Integer type) {
        if (page == null) {
            page = 1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        QueryWrapper<JcBasicdatavalue> queryWrapper=new QueryWrapper<>();
        if(type!=null){
            queryWrapper.eq("type",type);
        }
        return jcBasicdatavalueMapper.selectPage(new Page<>(page,pageSize),queryWrapper);
    }

    //获取所有的基本数据类型的枚举常量
    @Override
    public JcBasicdatavalue.Type[] listBasicdatavalueTypes() {
        return JcBasicdatavalue.Type.values();
    }


    //添加基本数据
    @Override
    public int addBasicdatavalue(JcBasicdatavalue basicdatavalue) {
        int row=jcBasicdatavalueMapper.insert(basicdatavalue);
        return row;
    }

    //验证添加的基本数据是否已经存在
    @Override
    public boolean checkBasicdataName(String name) {
        QueryWrapper<JcBasicdatavalue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        JcBasicdatavalue basicdatavalue= jcBasicdatavalueMapper.selectOne(queryWrapper);
        if(basicdatavalue==null){
            return true;
        }
        return false;
    }


    //根据oid查询基本数据
    @Override
    public JcBasicdatavalue getBasicdatavalueById(Integer oid) {
        JcBasicdatavalue basicdatavalue=jcBasicdatavalueMapper.selectById(oid);
        return basicdatavalue;
    }


    //根据名称查询基本数据
    @Override
    public JcBasicdatavalue getBasicdatavalueByName(String name) {
        QueryWrapper<JcBasicdatavalue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return jcBasicdatavalueMapper.selectOne(queryWrapper);
    }

    //对修改的基本数据进行验证
    @Override
    public boolean checkUpdateBasicdata(JcBasicdatavalue basicdatavalue) {
        int oid=basicdatavalue.getOid();
        String name=basicdatavalue.getName();
        JcBasicdatavalue basicdatavalue2=getBasicdatavalueByName(name);
        boolean s=true;
        //若名称重复
        if(name.equals(basicdatavalue2.getName())){
            //如果修改的基本数据和与其同名的基本数据不是同一个则拦截,否则在
            //不修改名称的情况下修改其他属性会被拦截。
            if(oid!=basicdatavalue2.getOid()){
                s=false;
            }
        }
        return s;
    }

    //修改基本数据
    @Override
    public int updateBasicdatavalue(JcBasicdatavalue basicdatavalue) {
        return jcBasicdatavalueMapper.updateById(basicdatavalue);
    }

    //删除基本数据
    @Override
    public boolean deleteBasicdatavalue(Integer oid) {
        try {
            jcBasicdatavalueMapper.deleteById(oid);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //查询所有职位状态为Y
    @Override
    public List<JcBasicdatavalue> getJobs() {
        QueryWrapper<JcBasicdatavalue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("state",'Y');
        queryWrapper.eq("type",'1');
        return jcBasicdatavalueMapper.selectList(queryWrapper);
    }

    //查询所有学历，状态为Y
    @Override
    public List<JcBasicdatavalue> getDegrees() {
        QueryWrapper<JcBasicdatavalue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",'Y');
        queryWrapper.eq("type",'3');
        return jcBasicdatavalueMapper.selectList(queryWrapper);
    }


}
